﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Windows.Threading;
using System.Windows;

namespace TRUONG_QUOC_PHIEN_SAI_GLTest
{
    class GiaCam : Image
    {
        BitmapImage bi, biSP;
        int speed = 1;
        int length = 710;
        int timeToDrop = 0;
        int randomToDrop;
        int GW = 778, GH = 459;
        int timeDropping = 13;
        public SanPham sp;
        public Thread thread;

        public GiaCam(string tenGiaCam)
        {
            this.Height = 88;
            this.Width = 82;

            bi = new BitmapImage();
            if (tenGiaCam == "GA")
            {
                bi.BeginInit();
                bi.UriSource = new Uri("Images/ga.png", UriKind.RelativeOrAbsolute);
                bi.EndInit();
            }

            if (tenGiaCam == "VIT")
            {
                bi.BeginInit();
                bi.UriSource = new Uri("Images/vit.png", UriKind.RelativeOrAbsolute);
                bi.EndInit();
            }

            if (tenGiaCam == "NGAN")
            {
                bi.BeginInit();
                bi.UriSource = new Uri("Images/ngan.png", UriKind.RelativeOrAbsolute);
                bi.EndInit();
            }
            this.Source = bi;
            this.Margin = new Thickness(0, 0, GW - this.Width, GH - this.Height);

            sp = new SanPham();

            thread = new Thread(new ThreadStart(runThread));
            randomToDrop = new Random().Next(0, 500);
        }

        private void runThread()
        {
            while (true)
            {
                for (int i = 0; i < length; i++)
                {
                    timeToDrop += 1;
                    this.Dispatcher.BeginInvoke
                        (
                            DispatcherPriority.Normal,
                            (ThreadStart)delegate()
                            {
                                
                                this.Margin = new Thickness(this.Margin.Left + speed, this.Margin.Top, this.Margin.Right - speed, this.Margin.Bottom);

                                if (timeToDrop == randomToDrop)
                                {
                                    biSP = new BitmapImage();
                                    Random rd = new Random();
                                    if (rd.Next(0, 4) == 0)
                                    {
                                        biSP.BeginInit();
                                        biSP.UriSource = new Uri("Images/shit.png", UriKind.RelativeOrAbsolute);
                                        biSP.EndInit();
                                        sp.type = 0;
                                    }
                                    else
                                    {
                                        biSP.BeginInit();
                                        biSP.UriSource = new Uri("Images/egg.png", UriKind.RelativeOrAbsolute);
                                        biSP.EndInit();
                                        sp.type = 1;
                                    }

                                    sp.Source = biSP;
                                    sp.Margin = new Thickness(this.Margin.Left - (this.Width/2) + (sp.Width/2), this.Height, this.Margin.Right - (this.Width/2) + (sp.Width/2), this.Margin.Bottom - sp.Height);
                                    timeToDrop = 0;
                                    randomToDrop = 500;
                                    sp.thread = new Thread(new ThreadStart(runThreadSP));
                                    sp.thread.Start();
                                }
                            }
                        );
                    Thread.Sleep(10);
                }
                speed = -speed;
            }
        }

        private void runThreadSP()
        {
            for (int i = 0; i < 380; i++)
            {
                sp.Dispatcher.BeginInvoke
                    (
                        DispatcherPriority.Normal,
                        (ThreadStart)delegate()
                        {
                            sp.Margin = new Thickness(sp.Margin.Left, sp.Margin.Top + MainWindow.level, sp.Margin.Right, sp.Margin.Bottom - MainWindow.level);
                            if (sp.Margin.Top >= MainWindow.temp_Image.Margin.Top
                                && sp.Margin.Left >= MainWindow.temp_Image.Margin.Left - 70
                                && sp.Margin.Right >= MainWindow.temp_Image.Margin.Right - 70
                                && sp.Margin.Bottom >= MainWindow.temp_Image.Margin.Bottom)
                            {
                                sp.thread.Abort();
                                sp.Source = null;
                                if (sp.type == 1)
                                {
                                    MainWindow.eggSum += 1;
                                    if (MainWindow.eggSum == 20)
                                    {
                                        MainWindow.eggSum = 0;
                                        MainWindow.level += 1;
                                    }
                                }
                                else
                                {
                                    MainWindow.live -= 1;
                                }
                            }
                            else if (i == 379)
                            {
                                if (sp.type == 1)
                                {
                                    MainWindow.live -= 1;
                                    if (MainWindow.live == 0)
                                    {
                                        sp.thread.Abort();
                                    }
                                }
                            }
                        }
                    );
                Thread.Sleep(timeDropping);
            }
        }
    }
}
